package com.newegg.app.qec.activity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import com.newegg.app.qec.R;
import com.newegg.app.qec.fragment.MineFragment;
import com.newegg.app.qec.util.BaseActivity;
import com.newegg.app.qec.util.Constants;
import com.newegg.app.qec.util.ImageUtil;
import com.newegg.app.qec.util.MyDialog;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Calendar;

/**
 * Created by Allen on 2017/12/8.
 */
public class PersonInfoActivity extends BaseActivity implements View.OnClickListener {

    private TextView setdate_tv;
    private TextView sex_tv;
    private ImageView head_picture;
    private TextView save_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personinfo);
        setdate_tv = (TextView) findViewById(R.id.setdate_tv);
        sex_tv = (TextView) findViewById(R.id.sex_tv);
        head_picture = (ImageView) findViewById(R.id.head_picture);
        save_tv = (TextView) findViewById(R.id.save_tv);
        setdate_tv.setOnClickListener(this);
        sex_tv.setOnClickListener(this);
        head_picture.setOnClickListener(this);
        save_tv.setOnClickListener(this);

        //如果有头像，刚开始默认显示以前的头像
        String photo = Constants.getHeadPhotoByName(PersonInfoActivity.this, "photo");
        if (photo != null && photo.length() != 0 && !photo.equals("")) {
            ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(photo.getBytes(), Base64.DEFAULT));
            Drawable drawable = Drawable.createFromStream(bais, "");
            head_picture.setImageDrawable(drawable);
        } else {
            head_picture.setImageResource(R.mipmap.head_picture);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setdate_tv:
                Calendar instance = Calendar.getInstance();
                DatePickerDialog pickerDialog = new DatePickerDialog(PersonInfoActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            private StringBuffer stringBuffer;

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int monthOfYear, int dayOfMonth) {
                                stringBuffer = new StringBuffer("");
                                stringBuffer.append(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                                setdate_tv.setText("" + stringBuffer);
                            }
                        }, instance.get(Calendar.YEAR),
                        instance.get(Calendar.MONTH),
                        instance.get(Calendar.DAY_OF_MONTH));
                pickerDialog.setMessage("请选择时间");
                pickerDialog.setCancelable(false);
                pickerDialog.show();
                break;
            case R.id.sex_tv:
                final MyDialog dialog = new MyDialog();
                dialog.sex_Dialog(PersonInfoActivity.this, R.layout.sex_dialog);
                final TextView man_tv = dialog.getContent_tv1();
                final TextView woman_tv = dialog.getContent_tv2();
                final TextView secrecy_tv = dialog.getContent_tv3();

                man_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        sex_tv.setText(man_tv.getText());
                        dialog.dismiss();
                    }
                });
                woman_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        sex_tv.setText(woman_tv.getText());
                        dialog.dismiss();
                    }
                });
                secrecy_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        sex_tv.setText(secrecy_tv.getText());
                        dialog.dismiss();
                    }
                });

                break;
            case R.id.head_picture:
                final MyDialog myDialog = new MyDialog();
                myDialog.photo_Dialog(PersonInfoActivity.this, R.layout.head_picture_dialog);
                TextView content_tv1 = myDialog.getContent_tv1();
                TextView content_tv2 = myDialog.getContent_tv2();
                TextView cancel_tv = myDialog.getCancel_tv();
                content_tv1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        //相机有默认的存储路径，如果指定路径，返回来的data为空
                        //下面这句指定调用相机拍照后的照片存储的路径
//                        takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mTempPhotoPath)));
                        startActivityForResult(takeIntent, 1);
                        myDialog.dismiss();
                    }
                });
                content_tv2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
                        // 如果限制上传到服务器的图片类型时可以直接写如："image/jpeg 、 image/png等的类型"
                        pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                        startActivityForResult(pickIntent, 2);
                        myDialog.dismiss();
                    }
                });
                cancel_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myDialog.dismiss();
                    }
                });
                break;
            case R.id.save_tv:
                sendBroadcast(new Intent("QEC").putExtra("QEC", 0));
                PersonInfoActivity.this.finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == PersonInfoActivity.this.RESULT_OK) {
            switch (requestCode) {
                case 1:
                    // TODO: 调用相机拍照
                    Bundle bundle = data.getExtras();
                    Bitmap picture = (Bitmap) bundle.get("data");
                    head_picture.setImageBitmap(picture);
                    //将图片存入SharedPreferences
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    picture.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
                    String imageBase64 = new String(Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT));
                    Constants.setHeadPhotoByName(PersonInfoActivity.this, "photo", imageBase64);
                    break;
                case 2:
                    // TODO: 直接从相册获取
                    Bitmap bitmap = null;
                    //判断手机系统版本号
                    if (Build.VERSION.SDK_INT >= 19) {
                        //4.4及以上系统使用这个方法处理图片
                        bitmap = ImageUtil.handleImageOnKitKat(this, data);        //ImgUtil是自己实现的一个工具类
                    } else {
                        //4.4以下系统使用这个方法处理图片
                        bitmap = ImageUtil.handleImageBeforeKitKat(this, data);
                    }
                    head_picture.setImageBitmap(bitmap);
                    //将图片存入SharedPreferences
                    ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream2);
                    String imageBase642 = new String(Base64.encodeToString(outputStream2.toByteArray(), Base64.DEFAULT));
                    Constants.setHeadPhotoByName(PersonInfoActivity.this, "photo", imageBase642);
                    break;

            }
        }
    }


}
